/**
 * IObject<T>在type/global.d.ts中定义，不需要引入即可直接使用
 * @param name 
 * @param data 
 * @param pExpires 
 */
export function setLocal(name:string,params:IObject<any>, pExpires = 1000 * 60 * 60 * 24):void {
  const d = params as ILocalStore
  d.startTime = Date.now()
  d.expires = pExpires
  console.log(d)
  localStorage.setItem(name,JSON.stringify(params))
}

/**
 * 获取localStorage对象并转换对应的类型
 * @param name 设置名称
 */
export function getLocal<T>(name:string):T {
  const ls = localStorage.getItem(name)
  const local = JSON.parse(ls !== null ? ls : "{}") as unknown as T
  return local
}

/**
 * 判断localStorage有效期是否失效
 * @param name 
 */
export function useLocal(name:string):Promise<ILocalStore> {
  return new Promise((resolve,reject) => {
    const local = getLocal<ILocalStore>(name)
    if(local.startTime + local.expires < Date.now()) reject(`${name}已超过有效期`)
    resolve(local)
  })
}